package com.xrh.xdemo.service.controllers;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.enums.ResultCodeEnum;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.entity.SysDictDTO;
import com.xrh.xdemo.api.request.addRequest.SysDictSaveRequest;
import com.xrh.xdemo.api.request.queryRequest.SysDictQueryRequest;
import com.xrh.xdemo.service.service.SysDictService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags="字典API")
@RestController
@RequestMapping("sysDict")
@Slf4j
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    /**
   	 * @function 保存字典
   	 * @param saveRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("保存字典")
   	@PostMapping("/saveSysDict")
   	public  CommonResult<Void> saveSysDict(@Valid @RequestBody SysDictSaveRequest saveRequest){
   		
   		try {
   		
   			sysDictService.saveSysDict(saveRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("saveSysDict 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 根据参数获取配置信息
   	 * @author 肖荣辉
   	 * @param sysDictQueryRequest
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("根据参数获取配置信息")
   	@PostMapping("/findConfigInfo")
   	public CommonResult<String> findConfigInfo(@RequestBody  SysDictQueryRequest sysDictQueryRequest){
   		
   		try {
   			 
   			String configInfo = sysDictService.findConfigByType(sysDictQueryRequest.getType(), sysDictQueryRequest.getBusinessKey());
   			
   			return CommonResult.success(configInfo);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findConfigInfo 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 查询字典列表
   	 * @author 肖荣辉
   	 * @param sysRoleQueryRequest
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取字典列表")
   	@PostMapping("/findSysDictList")
   	public CommonResult<CommonPage<SysDictDTO>> findSysDictList(@RequestBody SysDictQueryRequest queryRequest){
   		
   		try {
   			 
   			return sysDictService.findSysDictList(queryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findSysRoleList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}

}
